package com.cloud.pole.user.biz.account;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ace.cache.annotation.Cache;
import com.ace.cache.annotation.CacheClear;
import com.cloud.pole.api.user.UserAccountApi;
import com.cloud.pole.common.biz.BaseBiz;
import com.cloud.pole.mapper.account.UserAccountMapper;
import com.cloud.pole.user.model.entity.account.UserAccount;
import com.cloud.pole.user.model.vo.account.UserAccountVo;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-08 16:23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserAccountBiz extends BaseBiz<UserAccountMapper, UserAccount> {
	
	@Autowired
	UserAccountMapper userAccountMapper;
	
	/**
	 * 添加返回值
	 * @param entity
	 */
    public void insertUseGeneratedKeys(UserAccount entity) {
		userAccountMapper.insertUseGeneratedKeys(entity);
    }
    
    public UserAccount selectByPrimaryKey(Long id){
        return mapper.selectByPrimaryKey(id);
    }
    
    @Cache(key="userAccount{1}userId{2}typeId")
    public UserAccount getUserAccountByTypeId(Long userId, Integer typeId){
    	UserAccount account = new UserAccount();
    	account.setUserId(userId);
    	account.setTypeId(typeId);
        return mapper.selectOne(account);
    }
    
    @CacheClear(keys= {"userAccount{1}userId", "userAccountList{1}"})
    public int updateBalanceReduce(Long userId, Integer typeId, BigDecimal amount){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("userId", userId);
    	map.put("typeId", typeId);
    	map.put("amount", amount);
    	return mapper.updateBalanceReduce(map);
    }
    
    @CacheClear(keys= {"userAccount{1}userId", "userAccountList{1}"})
    public int updateBalancePlus(Long userId, Integer typeId, BigDecimal amount){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("userId", userId);
    	map.put("typeId", typeId);
    	map.put("amount", amount);
    	return mapper.updateBalancePlus(map);
    }
    
	public List<UserAccountVo> selectBalanceListByUserId(Long userId){
		return userAccountMapper.selectBalanceListByUserId(userId);
	}
}
